home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / Source / Chapter 5 / Engine / Input.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-10-01  |  5.5 KB  |  168 lines

  1. //-----------------------------------------------------------------------------
  2. // Input.h implementation.
  3. // Refer to the Input.h interface for more details.
  4. //
  5. // Programming a Multiplayer First Person Shooter in DirectX
  6. // Copyright (c) 2004 Vaughan Young
  7. //-----------------------------------------------------------------------------
  8. #include "Engine.h"
  9.  
  10. //-----------------------------------------------------------------------------
  11. // The input class constructor.
  12. //-----------------------------------------------------------------------------
  13. Input::Input( HWND window )
  14. {
  15.     // Store the handle to the parent window.
  16.     m_window = window;
  17.  
  18.     // Create a DirectInput interface.
  19.     DirectInput8Create( GetModuleHandle( NULL ), DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&m_di, NULL );
  20.  
  21.     // Create, prepare, and aquire the keyboard device.
  22.     m_di->CreateDevice( GUID_SysKeyboard, &m_keyboard, NULL );
  23.     m_keyboard->SetDataFormat( &c_dfDIKeyboard );
  24.     m_keyboard->SetCooperativeLevel( m_window, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE );
  25.     m_keyboard->Acquire();
  26.  
  27.     // Create, prepare, and aquire the mouse device.
  28.     m_di->CreateDevice( GUID_SysMouse, &m_mouse, NULL );
  29.     m_mouse->SetDataFormat( &c_dfDIMouse );
  30.     m_mouse->SetCooperativeLevel( m_window, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE );
  31.     m_mouse->Acquire();
  32.  
  33.     // Start the press stamp.
  34.     m_pressStamp = 0;
  35. }
  36.  
  37. //-----------------------------------------------------------------------------
  38. // The input class destructor.
  39. //-----------------------------------------------------------------------------
  40. Input::~Input()
  41. {
  42.     SAFE_RELEASE( m_di );
  43.     SAFE_RELEASE( m_keyboard );
  44.     SAFE_RELEASE( m_mouse );
  45. }
  46.  
  47. //-----------------------------------------------------------------------------
  48. // Updates the state of both the keyboard and mouse device.
  49. //-----------------------------------------------------------------------------
  50. void Input::Update()
  51. {
  52.     static HRESULT result;
  53.  
  54.     // Poll the keyboard until it succeeds or returns an unknown error.
  55.     while( true )
  56.     {
  57.         m_keyboard->Poll();
  58.         if( SUCCEEDED( result = m_keyboard->GetDeviceState( 256, (LPVOID)&m_keyState ) ) )
  59.             break;
  60.         if( result != DIERR_INPUTLOST && result != DIERR_NOTACQUIRED )
  61.             return;
  62.  
  63.         // Reacquire the device if the focus was lost.
  64.         if( FAILED( m_keyboard->Acquire() ) )
  65.             return;
  66.     }
  67.  
  68.     // Poll the mouse until it succeeds or returns an unknown error.
  69.     while( true )
  70.     {
  71.         m_mouse->Poll();
  72.         if( SUCCEEDED( result = m_mouse->GetDeviceState( sizeof( DIMOUSESTATE ), &m_mouseState ) ) )
  73.             break;
  74.         if( result != DIERR_INPUTLOST && result != DIERR_NOTACQUIRED )
  75.             return;
  76.  
  77.         // Reacquire the device if the focus was lost.
  78.         if( FAILED( m_mouse->Acquire() ) )
  79.             return;
  80.     }
  81.  
  82.     // Get the relative position of the mouse.
  83.     GetCursorPos( &m_position );
  84.     ScreenToClient( m_window, &m_position );
  85.  
  86.     // Increment the press stamp.
  87.     m_pressStamp++;
  88. }
  89.  
  90. //-----------------------------------------------------------------------------
  91. // Returns true if the given key is pressed.
  92. // Note: Consistent presses will return false when using the press stamp.
  93. //-----------------------------------------------------------------------------
  94. bool Input::GetKeyPress( char key, bool ignorePressStamp )
  95. {
  96.     if( ( m_keyState[key] & 0x80 ) == false )
  97.         return false;
  98.  
  99.     bool pressed = true;
  100.  
  101.     if( ignorePressStamp == false )
  102.         if( m_keyPressStamp[key] == m_pressStamp - 1 || m_keyPressStamp[key] == m_pressStamp )
  103.             pressed = false;
  104.  
  105.     m_keyPressStamp[key] = m_pressStamp;
  106.  
  107.     return pressed;
  108. }
  109.  
  110. //-----------------------------------------------------------------------------
  111. // Returns true if the given button is pressed.
  112. // Note: Consistent presses will return false when using the press stamp.
  113. //-----------------------------------------------------------------------------
  114. bool Input::GetButtonPress( char button, bool ignorePressStamp )
  115. {
  116.     if( ( m_mouseState.rgbButtons[button] & 0x80 ) == false )
  117.         return false;
  118.  
  119.     bool pressed = true;
  120.  
  121.     if( ignorePressStamp == false )
  122.         if( m_buttonPressStamp[button] == m_pressStamp - 1 || m_buttonPressStamp[button] == m_pressStamp )
  123.             pressed = false;
  124.  
  125.     m_buttonPressStamp[button] = m_pressStamp;
  126.  
  127.     return pressed;
  128. }
  129.  
  130. //-----------------------------------------------------------------------------
  131. // Returns the x position of the mouse.
  132. //-----------------------------------------------------------------------------
  133. long Input::GetPosX()
  134. {
  135.     return m_position.x;
  136. }
  137.  
  138. //-----------------------------------------------------------------------------
  139. // Returns the y position of the mouse.
  140. //-----------------------------------------------------------------------------
  141. long Input::GetPosY()
  142. {
  143.     return m_position.y;
  144. }
  145.  
  146. //-----------------------------------------------------------------------------
  147. // Returns the change in the mouse's x coordinate.
  148. //-----------------------------------------------------------------------------
  149. long Input::GetDeltaX()
  150. {
  151.     return m_mouseState.lX;
  152. }
  153.  
  154. //-----------------------------------------------------------------------------
  155. // Returns the change in the mouse's y coordinate.
  156. //-----------------------------------------------------------------------------
  157. long Input::GetDeltaY()
  158. {
  159.     return m_mouseState.lY;
  160. }
  161.  
  162. //-----------------------------------------------------------------------------
  163. // Returns the change in the mouse's scroll wheel.
  164. //-----------------------------------------------------------------------------
  165. long Input::GetDeltaWheel()
  166. {
  167.     return m_mouseState.lZ;
  168. }